﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sands.Common;
using Sands.Web;
using Sands.Controls;
using Sands.DataAccess;
using SAWeb.Model;
using SAWeb.Service;


namespace SAWeb.Web
{
    public partial class UsersInfo : Page
    {
        /// <summary>
        /// 条件，自己改
        /// </summary>
        protected Clip Where { get { return null ; } }
        /// <summary>
        /// 排序，自己改
        /// </summary>
        protected Clip OrderBy { get { return  Db.Users.UserId.Desc ; } }

		protected void Page_Load(object sender, EventArgs e)
        {
			rptList.BindData =  (pageIndex, pageSize) =>
		    {
			    using (var service = new UsersService())
			    {
				    return service.GetPageList(pageIndex, pageSize, Where,OrderBy);
			    }
		    };

			if (!IsPostBack)
            {
				rptList.ReBind();
            }
        }

          protected void FormEvent(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "add":
                    pnlDetail.GetEdit(new Users());
                    pnlList.Visible = false;
                    break;
                case "save":
                    "保存".InfoAction(() =>
                    {
                       using (var service = new UsersService())
						{
							int id = hfUserId.Value.ToInt();
							var model = service.GetModelById(id);
							model.GetData(pnlDetail);
							model.Save(service.DataContext);
							pnlDetail.Hide();
							pnlList.Visible = true;
							rptList.ReBind(rptList.PageIndex);
                        }
                    });
                    break;
                case "cancel":
                    pnlDetail.Hide();
                    pnlList.Visible = true;
                    break;
                case "batchdel":
                   using (var service = new UsersService())
                   {
                     "批量删除".InfoAction(() =>
                     {
					      service.Delete(Db.Users.UserId.In(rptList.GetSelectValues().Select<string, object>(s => s.ToInt()).ToArray()));
                          rptList.ReBind();
                      });
                    }
                    break;
                case "search":
                    rptList.ReBind();
                    break;
            }
        }  

	
        protected void rptList_Command(object sender, RepeaterCommandEventArgs e)
        {
            int id = e.CommandArgument.ToString().ToInt();
            string cmd = e.CommandName.ToLower();
			using (var service = new UsersService())
				{
				switch (cmd)
				{
					case "delete":
						"删除".InfoAction( () =>
						{
						 service.Delete(Db.Users.UserId == id);
						 rptList.ReBind(rptList.PageIndex);
						});
						break;
					case "edit":
					  "获取编辑信息".InfoAction( () =>
						{
							pnlDetail.GetEdit(service.GetModelById(id));
						} , ActionShowMsgType.ShowError);
						pnlList.Visible = false;
						break;
				}
			}
        }
    }
}